package net.hn.hnms.biz.upkafka;

import net.hn.hnms.biz.upkafka.config.KafkaProperties;
import net.hn.hnms.biz.upkafka.config.LoginUtil;
import net.hn.hnms.biz.upkafka.config.ServerConfig;
import net.hn.hnms.biz.upkafka.config.SpringContextUtil;
import net.hn.hnms.biz.upkafka.service.*;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;

import java.io.IOException;


@SpringBootApplication(scanBasePackages = "net.hn.hnms")
//@EnableScheduling // 启用定时任务
@ComponentScan(basePackages = {"net.hn.hnms"})
@MapperScan({"net.hn.hnms.biz.upkafka.mapper"})
@EnableAsync
public class UpKafkaApplication {

//    @Resource
//    private ServerConfig serverConfig;
    public static void main(String[] args) {
        SpringApplication.run(UpKafkaApplication.class, args);
        KafkaProperties.getInstance();
        // 从 Spring 上下文中获取 ServerConfig Bean
        ServerConfig serverConfig = SpringContextUtil.getBean(ServerConfig.class);

        String mineCode = serverConfig.getMineCode();
        System.out.println("启动煤矿编码: " + mineCode);

        String mineNameByMineCode = KafkaProperties.getMineNameByMineCode(mineCode);
        System.out.println("mineNameByMineCode = " + mineNameByMineCode);
        //用户账号
        String user = "";
        //用户密码
        String keytab = "";
        if ("150781013671".equals(mineCode)) {
            //150781013671=灵露煤矿
            user = KafkaProperties.USER_PRINCIPAL_LINGLU;
            keytab = KafkaProperties.USER_KEYTAB_FILE;
        } else if ("150621011015".equals(mineCode)) {
            //150621011015=高头窑煤矿
            user = KafkaProperties.USER_PRINCIPAL_GAOTOUYAO;
            keytab = KafkaProperties.USER_KEYTAB_FILE;
        } else if ("".equals(mineCode)) {
            //620824009734=砚北煤矿
            user = KafkaProperties.USER_PRINCIPAL_YANBEI;
            keytab = KafkaProperties.USER_KEYTAB_FILE;
        } else {
            //默认灵露
            user = KafkaProperties.USER_PRINCIPAL_LINGLU;
            keytab = KafkaProperties.USER_KEYTAB_FILE;
        }
        // 是否使用异步发送模式
        final boolean asyncEnable = false;
        if (LoginUtil.isSecurityModel()) {
            try {
                // !!注意，安全认证时，需要用户手动修改为自己申请的机机账号
                LoginUtil.securityPrepare(user, keytab);
            } catch (IOException e) {
                System.out.println("e = " + e);
                return;
            }
            System.out.println("Security prepare success");
        }

//        eg:实例
//        Producer producerThread = new Producer(KafkaProperties.ZCXT_ZCSS_TOPIC_tmp, asyncEnable,"150781013671");
//        producerThread.start();
        ZCProducer zcProducer = new ZCProducer(KafkaProperties.ZCXT_ZCSS_TOPIC_tmp, asyncEnable,mineCode);
        zcProducer.start();
        ZTFProducer ztfProducer = new ZTFProducer(KafkaProperties.ZTF_ZTFSS_TOPIC, asyncEnable,mineCode);
        ztfProducer.start();
        XJProducer xjProducer = new XJProducer(KafkaProperties.ZTF_XJSS_TOPIC, asyncEnable,mineCode);
        xjProducer.start();
        if ("150781013671".equals(mineCode) || "150621011015".equals(mineCode)) {
            //需要上传ftp 灵露 高头窑
            AQJKProducer aqjkProducer = new AQJKProducer("aqjk", asyncEnable,mineCode);
            aqjkProducer.start();
        }

    }

}
